package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.bean.DailyPlanBean;
import com.yanchangapp.model.request.PlanHistoryRequest;
import com.yanchangapp.ui.view.HistoryPlanAtView;
import com.zxl.baselib.bean.response.BasePageListResponse;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-10-15.
 * description:
 */
public class HistoryPlanAtPresenter extends BasePresenter<HistoryPlanAtView> {

    public HistoryPlanAtPresenter(BaseActivity context) {
        super(context);
    }

    public void queryHistoryPlan(String startTime,String endTime){
        HeadApiRetrofit.getInstance().postQueryHistoryPlans(new PlanHistoryRequest(startTime,endTime))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BasePageListResponse<DailyPlanBean>>() {
                    @Override
                    public void onNext(BasePageListResponse<DailyPlanBean> dailyPlanBeanBasePageListResponse) {
                        if (getView() != null){
                            if (AppConstant.REQUEST_SUCCESS == dailyPlanBeanBasePageListResponse.getCode()){
                                getView().queryHistoryList(dailyPlanBeanBasePageListResponse.getData().getList());
                            }else {
                                getView().queryHistoryFailure(dailyPlanBeanBasePageListResponse.getMsg());
                            }
                        }

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        if (getView() != null){
                            getView().queryHistoryFailure("网络错误");
                        }
                    }
                });
    }
}
